home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / InterLaunch 1.1.2 / src / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-06  |  11.3 KB  |  515 lines  |  [TEXT/CWIE]

  1. /* ----------------------------------------------------------------------
  2.  
  3.     Launcher
  4.     
  5.     Written by: Paul Celestin
  6.     
  7.     950314 - 1.0.0 - absolute first version
  8.     950321 - 1.0.1 - work continues to add a few more features
  9.     950808 - 1.1.0 - added ability to open ConfigPPP
  10.     951213 - 1.1.1 - fixed (maybe) crashing bug when launching apps
  11.     960704 - 1.1.2 - updated to compile with CW9
  12.  
  13. ---------------------------------------------------------------------- */
  14.  
  15.  
  16. /* ----------------------------------------------------------------------
  17. includes
  18. ---------------------------------------------------------------------- */
  19.  
  20. #include    "the_defines.h"
  21. #include    "the_globals.h"
  22. #include    "the_prototypes.h"
  23. #include    "pppcontrol.h"
  24.  
  25. Boolean                gDone,
  26.                     gWNEImplemented,
  27.                     gInBackground;
  28. int                    gNumApps;
  29. EventRecord            gTheEvent;
  30. MenuHandle            gAppleMenu,
  31.                     gFileMenu;
  32. WindowPtr            gTheWindow;
  33. PixPatHandle        gPixPat;
  34. ProcessSerialNumber    gMailApp,
  35.                     gNewsApp,
  36.                     gFTPApp,
  37.                     gWebApp,
  38.                     gTelnetApp;
  39.  
  40. /* ----------------------------------------------------------------------
  41. main - here is where it all began...
  42. ---------------------------------------------------------------------- */
  43. void main()
  44. {
  45.     InitToolBox();
  46.     InitMenuBar();
  47.     MainLoop();
  48.     ExitToShell();
  49. }
  50.  
  51.  
  52. /* ----------------------------------------------------------------------
  53. InitToolBox
  54. ---------------------------------------------------------------------- */
  55. void InitToolBox()
  56. {
  57.     InitGraf(&qd.thePort);
  58.     InitFonts();
  59.     FlushEvents(everyEvent, 0);
  60.     InitWindows();
  61.     InitMenus();
  62.     TEInit();
  63.     InitDialogs(0L);
  64.     InitCursor();
  65. }
  66.  
  67.  
  68. /* ----------------------------------------------------------------------
  69. InitMenuBar
  70. ---------------------------------------------------------------------- */
  71. void InitMenuBar()
  72. {
  73.     Handle    myMenuBar;
  74.     
  75.     myMenuBar = GetNewMBar(MENU_BASE_ID);
  76.     if (myMenuBar == NIL)
  77.         SysBeep(1);
  78.     else
  79.         SetMenuBar(myMenuBar);
  80.     
  81.     gAppleMenu = GetMHandle(MENU_APPLE_ID);
  82.     if (gAppleMenu == NIL)
  83.         SysBeep(1);
  84.     else
  85.         AddResMenu(gAppleMenu,'DRVR');
  86.     
  87.     gFileMenu = GetMHandle(MENU_FILE_ID);
  88.     if (gFileMenu == NIL)
  89.         SysBeep(1);
  90.  
  91.     DrawMenuBar();
  92. }
  93.  
  94.  
  95. /* ----------------------------------------------------------------------
  96. MainLoop
  97. ---------------------------------------------------------------------- */
  98. void MainLoop()
  99. {
  100.     RgnHandle        cursorRgn;
  101.     Boolean            gotEvent;
  102.     WindowRecord    *myWinRecord;
  103.     Rect            myWinRect;
  104.     Str255             myTitle;
  105.     
  106.     gInBackground = false;
  107.     
  108.     cursorRgn = NewRgn();
  109.     
  110.     gWNEImplemented = (NGetTrapAddress(WNE_TRAP_NUM,ToolTrap) !=
  111.         NGetTrapAddress(UNIMPL_TRAP_NUM,ToolTrap));
  112.  
  113.     gPixPat = GetPixPat(SYSTEM_PPAT);
  114.  
  115.     myWinRect = qd.screenBits.bounds;
  116.     myWinRecord = NIL;
  117.     gTheWindow =
  118.         NewCWindow(myWinRecord,&myWinRect,"\p",FALSE,plainDBox,(WindowPtr)-1L,FALSE,0L);
  119.     
  120.     if (gTheWindow)
  121.     {
  122.         DrawButtons();
  123.         ShowWindow(gTheWindow);
  124.         DoUpdate();
  125.  
  126.         gDone = false;
  127.  
  128.         while (gDone == false)
  129.         {
  130.             if (gWNEImplemented)
  131.                 gotEvent = WaitNextEvent(everyEvent,&gTheEvent,MIN_SLEEP,cursorRgn);
  132.             else
  133.             {
  134.                 SystemTask();
  135.                 gotEvent = GetNextEvent(everyEvent,&gTheEvent);
  136.             }
  137.         
  138.             if (gotEvent)
  139.                 Do();
  140.         }
  141.         QuitAllApps();
  142.     }
  143. }
  144.  
  145.  
  146. /* ----------------------------------------------------------------------
  147. DrawButtons
  148. ---------------------------------------------------------------------- */
  149. void DrawButtons()
  150. {
  151.     ControlHandle    hPPP, hMail, hNews, hFTP, hWeb, hTelnet;
  152.     Rect            tRect;
  153.     short            theCenter,numButtons,buttonWidth,
  154.                     stripWidth,curPosition,theTop;
  155.  
  156.     tRect = qd.screenBits.bounds;
  157.     theCenter = tRect.right / 2;
  158.     numButtons = 6;
  159.     buttonWidth = 64;
  160.     stripWidth = numButtons * buttonWidth;
  161.     curPosition = theCenter - (stripWidth / 2);
  162.     theTop = (tRect.bottom  / 1.5);
  163.  
  164.     tRect.top = theTop;
  165.     tRect.bottom = tRect.top + 64;
  166.     tRect.left = curPosition;
  167.     tRect.right = curPosition + buttonWidth;
  168.     hPPP = NewControl(gTheWindow,&tRect,"\pConfig PPP",1,PPP_CONTROL,0,0,577,0);
  169.     curPosition = curPosition + buttonWidth;
  170.  
  171.     tRect.top = theTop;
  172.     tRect.bottom = tRect.top + 64;
  173.     tRect.left = curPosition;
  174.     tRect.right = curPosition + buttonWidth;
  175.     hMail = NewControl(gTheWindow,&tRect,"\pMail",1,MAIL_CONTROL,0,0,577,0);
  176.     curPosition = curPosition + buttonWidth;
  177.  
  178.     tRect.top = theTop;
  179.     tRect.bottom = tRect.top + 64;
  180.     tRect.left = curPosition;
  181.     tRect.right = curPosition + buttonWidth;
  182.     hNews = NewControl(gTheWindow,&tRect,"\pNews",1,NEWS_CONTROL,0,0,577,0);
  183.     curPosition = curPosition + buttonWidth;
  184.  
  185.     tRect.top = theTop;
  186.     tRect.bottom = tRect.top + 64;
  187.     tRect.left = curPosition;
  188.     tRect.right = curPosition + buttonWidth;
  189.     hFTP = NewControl(gTheWindow,&tRect,"\pFTP",1,FTP_CONTROL,0,0,577,0);
  190.     HLock((Handle)hFTP);
  191.     curPosition = curPosition + buttonWidth;
  192.  
  193.     tRect.top = theTop;
  194.     tRect.bottom = tRect.top + 64;
  195.     tRect.left = curPosition;
  196.     tRect.right = curPosition + buttonWidth;
  197.     hWeb = NewControl(gTheWindow,&tRect,"\pWeb",1,WEB_CONTROL,0,0,577,0);
  198.     curPosition = curPosition + buttonWidth;
  199.  
  200.     tRect.top = theTop;
  201.     tRect.bottom = tRect.top + 64;
  202.     tRect.left = curPosition;
  203.     tRect.right = curPosition + buttonWidth;
  204.     hTelnet = NewControl(gTheWindow,&tRect,"\pTelnet",1,TELNET_CONTROL,0,0,577,0);
  205.     curPosition = curPosition + buttonWidth;
  206. }
  207.     
  208.  
  209. /* ----------------------------------------------------------------------
  210. Do
  211. ---------------------------------------------------------------------- */
  212. void Do()
  213. {
  214.     char    c;
  215.     
  216.     switch (gTheEvent.what)
  217.     {
  218.         case nullEvent:
  219.             break;
  220.         case mouseDown:
  221.             DoMouseDown();
  222.             break;
  223.         case keyDown:
  224.         case autoKey:
  225.             c = gTheEvent.message & charCodeMask;
  226.             if ((gTheEvent.modifiers & cmdKey) != 0)
  227.             {
  228.                 DoMenu(MenuKey(c));
  229.             }
  230.             break;
  231.         case activateEvt:
  232.             DoUpdate();
  233.             break;
  234.         case updateEvt:
  235.             DoUpdate();
  236.             break;
  237.         case app4Evt:
  238.             if ((gTheEvent.message & SUSPEND_RESUME_BIT) == RESUMING)
  239.                 gInBackground = (gTheEvent.message & 0x01) == 0;
  240.             break;
  241.     }
  242. }
  243.  
  244. /* ----------------------------------------------------------------------
  245. DoUpdate
  246. ---------------------------------------------------------------------- */
  247. void DoUpdate()
  248. {
  249.     GrafPtr            savedPort;
  250.     Rect            theRect,pictRect;
  251.     PicHandle        thePicture;
  252.     
  253.     SetCursor(&qd.arrow);
  254.     GetPort(&savedPort);
  255.     SetPort(gTheWindow);
  256.     BeginUpdate(gTheWindow);
  257.         SetRect(&theRect,gTheWindow->portRect.left,
  258.             gTheWindow->portRect.top,
  259.             gTheWindow->portRect.right,
  260.             gTheWindow->portRect.bottom);
  261.         if (gPixPat != NIL)
  262.             FillCRect(&theRect,gPixPat);
  263.  
  264.         if ((thePicture = GetPicture(BACKGROUND_PICT)) != NIL)
  265.         {
  266.             pictRect = (**(thePicture)).picFrame;
  267.             pictRect.left = (theRect.right / 2) - (pictRect.right / 2);
  268.             pictRect.right = pictRect.left + pictRect.right;
  269.             pictRect.top = (theRect.bottom / 4) - (pictRect.bottom / 4);
  270.             pictRect.bottom = (pictRect.top + pictRect.bottom);
  271.             DrawPicture(thePicture,&pictRect);
  272.         }
  273.         DrawControls(gTheWindow);
  274.     EndUpdate(gTheWindow);
  275.     SetPort(savedPort);
  276.     HPurge((Handle)thePicture);
  277. }
  278.  
  279. /* ----------------------------------------------------------------------
  280. DoMouseDown
  281. ---------------------------------------------------------------------- */
  282. void DoMouseDown()
  283. {
  284.     WindowPtr    window;
  285.     short int    thePart;
  286.     long int    menuChoice, windSize, newSize;
  287.     Boolean        doZoom, doGoAway;
  288.     Point        p;
  289.     
  290.     thePart = FindWindow(gTheEvent.where,&window);
  291.     switch (thePart)
  292.     {
  293.         case inMenuBar:
  294.             menuChoice = MenuSelect(gTheEvent.where);
  295.             DoMenu(menuChoice);
  296.             break;
  297.         case inSysWindow:
  298.             SystemClick(&gTheEvent,window);
  299.             break;
  300.         case inContent:
  301.             if (window != FrontWindow())
  302.                 SelectWindow(window);
  303.             else
  304.             {
  305.                 p = gTheEvent.where;
  306.                 GlobalToLocal(&p);
  307.                 DoWindow(p);
  308.             }
  309.             break;
  310.         default:
  311.             break;
  312.     }
  313. }
  314.  
  315. /* ----------------------------------------------------------------------
  316. DoMenu
  317. ---------------------------------------------------------------------- */
  318. void DoMenu(menuChoice)
  319. long int menuChoice;
  320. {
  321.     int    theMenu;
  322.     int    theItem;
  323.     
  324.     if (menuChoice != 0)
  325.     {
  326.         theMenu = HiWord(menuChoice);
  327.         theItem = LoWord(menuChoice);
  328.         switch (theMenu)
  329.         {
  330.             case MENU_APPLE_ID:
  331.                 DoMenuApple(theItem);
  332.                 break;
  333.             case MENU_FILE_ID:
  334.                 DoMenuFile(theItem);
  335.                 break;
  336.             default:
  337.                 break;
  338.         }
  339.         HiliteMenu(0);
  340.     }
  341. }
  342.  
  343. /* ----------------------------------------------------------------------
  344. DoWindow
  345. ---------------------------------------------------------------------- */
  346. void DoWindow(Point p)
  347. {
  348.     short            choice;
  349.     ControlHandle    control;
  350.     CursHandle        theCursor;
  351.     
  352.     if (FindControl(p,gTheWindow,&control))
  353.     {
  354.         if (TrackControl(control,p,NIL))
  355.         {
  356.             theCursor = GetCursor(4);
  357.             HLock((Handle)theCursor);
  358.             SetCursor(*theCursor);
  359.             HUnlock((Handle)theCursor);
  360.  
  361.             choice = GetCtlValue(control);
  362.             
  363.             switch (choice)
  364.             {
  365.                 case PPP_CONTROL:
  366.                     DoPPP();
  367.                     break;
  368.                 case MAIL_CONTROL:
  369.                     DoMail();
  370.                     break;
  371.                 case NEWS_CONTROL:
  372.                     DoNews();
  373.                     break;
  374.                 case FTP_CONTROL:
  375.                     DoFTP();
  376.                     break;
  377.                 case WEB_CONTROL:
  378.                     DoWeb();
  379.                     break;
  380.                 case TELNET_CONTROL:
  381.                     DoTelnet();
  382.                     break;
  383.                 default:
  384.                     break;
  385.             }
  386.         }
  387.     }
  388. }
  389.  
  390.  
  391. /* ----------------------------------------------------------------------
  392. DoPPP
  393. ---------------------------------------------------------------------- */
  394. void DoPPP()
  395. {
  396.     short    vRefNum;
  397.     long    dirID;
  398.     FSSpec    theApp;
  399.     OSErr    myErr;
  400.  
  401.     myErr = FindFolder(kOnSystemDisk, kControlPanelFolderType,
  402.         kDontCreateFolder, &vRefNum, &dirID);
  403.     if (myErr != noErr)
  404.     {
  405.         SysBeep(1);
  406.         return;
  407.     }
  408.     myErr = LocateFile(TYPE_CDEV, CREATOR_PPP, vRefNum, &theApp);
  409.     if (myErr != noErr)
  410.     {
  411.         SysBeep(1);
  412.         return;
  413.     }
  414.     myErr = OpenCP(theApp, CREATOR_PPP);
  415.     if (myErr != noErr)
  416.     {
  417.         SysBeep(1);
  418.         return;
  419.     }
  420. }
  421.  
  422.  
  423. /* ----------------------------------------------------------------------
  424. DoMail
  425. ---------------------------------------------------------------------- */
  426. void DoMail()
  427. {
  428.     FSSpec    theApp;
  429.  
  430.     if (LocateFile('APPL', CREATOR_MAIL, 0, &theApp) == noErr)
  431.         if (LaunchIt(theApp,&gMailApp) != noErr)
  432.             SysBeep(1);
  433. }
  434.  
  435.  
  436. /* ----------------------------------------------------------------------
  437. DoNews
  438. ---------------------------------------------------------------------- */
  439. void DoNews()
  440. {
  441.     FSSpec    theApp;
  442.  
  443.     if (!pppup())
  444.         pppopen();
  445.     if (LocateFile('APPL', CREATOR_NEWS, 0, &theApp) == noErr)
  446.         if (LaunchIt(theApp,&gNewsApp) != noErr)
  447.             SysBeep(1);
  448. }
  449.  
  450.  
  451. /* ----------------------------------------------------------------------
  452. DoFTP
  453. ---------------------------------------------------------------------- */
  454. void DoFTP()
  455. {
  456.     FSSpec    theApp;
  457.  
  458.     if (!pppup())
  459.         pppopen();
  460.     if (LocateFile('APPL', CREATOR_FTP, 0, &theApp) == noErr)
  461.         if (LaunchIt(theApp,&gFTPApp) != noErr)
  462.             SysBeep(1);
  463. }
  464.  
  465.  
  466. /* ----------------------------------------------------------------------
  467. DoWeb
  468. ---------------------------------------------------------------------- */
  469. void DoWeb()
  470. {
  471.     FSSpec    theApp;
  472.  
  473.     if (!pppup())
  474.         pppopen();
  475.     if (LocateFile('APPL', CREATOR_WEB, 0, &theApp) == noErr)
  476.         if (LaunchIt(theApp,&gWebApp) != noErr)
  477.             SysBeep(1);
  478. }
  479.  
  480.  
  481. /* ----------------------------------------------------------------------
  482. DoTelnet
  483. ---------------------------------------------------------------------- */
  484. void DoTelnet()
  485. {
  486.     FSSpec    theApp;
  487.  
  488.     if (!pppup())
  489.         pppopen();
  490.     if (LocateFile('APPL', CREATOR_TELNET, 0, &theApp) == noErr)
  491.         if (LaunchIt(theApp,&gTelnetApp) != noErr)
  492.             SysBeep(1);
  493. }
  494.  
  495.  
  496. /* ----------------------------------------------------------------------
  497. QuitAllApps
  498. ---------------------------------------------------------------------- */
  499. void QuitAllApps()
  500. {
  501.     CursHandle        theCursor;
  502.  
  503.     theCursor = GetCursor(4);
  504.     HLock((Handle)theCursor);
  505.     SetCursor(*theCursor);
  506.     HUnlock((Handle)theCursor);
  507.  
  508.     QuitIt(&gMailApp);
  509.     QuitIt(&gNewsApp);
  510.     QuitIt(&gFTPApp);
  511.     QuitIt(&gWebApp);
  512.     QuitIt(&gTelnetApp);
  513.     if (pppup())
  514.         pppclose(1);
  515. }